package com.yunji.order.context.builder;

import com.yunji.common.model.cache.ProductCacheDTO;
import com.yunji.coupon.dto.OrderAmtDTO;
import com.yunji.order.domain.OrderDetailDO;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author : peter-zhu
 * @date : 2025/3/12 14:10
 * @description : TODO
 **/
public class OrderDetailBuilder {
    private final OrderDetailDO orderDetail;

    private OrderDetailBuilder() {
        this.orderDetail = new OrderDetailDO();
    }

    public static OrderDetailBuilder builder() {
        return new OrderDetailBuilder();
    }

    public OrderDetailBuilder buildAmount(OrderAmtDTO orderAndCouponAmt) {
        orderDetail.setProductNums(orderAndCouponAmt.getProductNums());
        orderDetail.setCostPrice(orderAndCouponAmt.getCostPrice());
        orderDetail.setPrice(orderAndCouponAmt.getPrice());
        orderDetail.setSiteSalePrice(orderAndCouponAmt.getDiscountAmount());
        // 应收金额就是拿的是扣除优惠券金额的网点销售价(也叫做折扣价)
        orderDetail.setPayableAmount(orderAndCouponAmt.getPayableAmount());
        orderDetail.setPayableUnitAmount(orderDetail.getPayableAmount().divide(new BigDecimal(orderAndCouponAmt.getProductNums()),
                4, RoundingMode.DOWN));
        return this;
    }

    public OrderDetailBuilder buildProduct(ProductCacheDTO product) {
        orderDetail.setProductId(product.getId());
        orderDetail.setProductName(product.getProductName());
        orderDetail.setProductTypeName(product.getProductTypeName());
        orderDetail.setSku(product.getSku());
        orderDetail.setProductType(product.getProductType());
        orderDetail.setProductUnits(product.getUnitId());
        orderDetail.setSubTitle(product.getSubTitle());
        orderDetail.setAlbumPics(product.getAlbumPics());
        orderDetail.setUnitPrice(product.getPrice());
        orderDetail.setUnitSalePrice(product.getDiscountPrice());
        orderDetail.setCostUnitPrice(product.getCostPrice());
        return this;
    }


    public OrderDetailDO build() {
        return this.orderDetail;
    }
}
